/*
 * @Author: wcx
 * @Email: 1055127249@qq.com
 * @Date: 2025-01-22 14:22:59
 * @LastEditors: wcx 1055127249@qq.com
 * @LastEditTime: 2025-01-23 09:19:02
 * @FilePath: \hello-prisma\script.ts
 * @Description: 描述
 */
import express from 'express';
import { PrismaClient } from '@prisma/client'
import createcc from './prisma/create.js'

const app = express();
const prisma = new PrismaClient()
app.get('/users', async (req, res) => {
  const { page = 1, pageSize = 10 } = req.query;
  const skip = (Number(page) - 1) * Number(pageSize);
  const take = Number(pageSize);
  try {
    res.json(await prisma.user.findMany({ skip, take }));
  } catch (error) {
    res.status(500).json({ error });
  }
})
app.post('/user', async (req, res) => {
  try {
    const { name, email } = req.body;
    const user = await prisma.user.create({
      data: {
        name,
        email,
      },
    });
    res.status(201).json(user);
  } catch (error) {
    res.status(500).json({ error });
  }
});

app.post('/user', async (req, res) => {
  try {
    const { name, email } = req.body;
    const user = await prisma.user.create({
      data: {
        name,
        email,
      },
    });
    res.status(201).json(user);
  } catch (error) {
    res.status(500).json({ error: 'Internal Server Error' });
  }
});

app.get('/create', async (req, res) => {
  await createcc()
  res.status(201).json({ status: '成功' });
});

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  console.log(`Server is running on port ${PORT}`);
});